<?php

namespace app\api\model;

use think\Model;

class ImFriendGroupUser extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * Notes:批量添加好友分组用户
     * User: tangfen
     * Date: 2023/12/23
     * Time: 15:42
     * @param $params
     * @return bool
     */
    public function addFriendGroupUser($params)
    {
        $membersArray = explode(',', $params['touid']);

        $info = [];
        if (!empty($membersArray)) {
            foreach ($membersArray as $k => $v) {
                $info['touid'] = $v;
                $info['friend_group_id'] = $params['friend_group_id'];
                $info['create_time'] = time();
                $info['update_time'] = time();

                $data = $this->where(['touid' => $v, 'friend_group_id' => $params['friend_group_id']])->value('id');
                if (empty($data)) {
                    $this->insert($info);
                } else {
                    $this->where(['touid' => $v, 'friend_group_id' => $params['friend_group_id']])->update(['status' => 1, 'update_time' => time()]);
                }
            }
        }
        return true;
    }


    /**
     * Notes:获取分组用户列表
     * User: tangfen
     * Date: 2023/12/23
     * Time: 16:25
     */
    public function groupFriendUserList($params)
    {
        $where = [
            'fgs.friend_group_id' => $params['id'],
            'fgs.status' => 1
        ];

        $list = $this::alias('fgs')
            ->join('user u', 'u.id=fgs.touid', 'LEFT')
            ->where($where)
            ->field('fgs.*,u.nick,u.username')
            ->order('id desc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);
        return $list;

    }

    /**
     * Notes:添加分组用户
     * User: tangfen
     * Date: 2023/12/23
     * Time: 16:11
     * @return bool
     */
    public function addFriendGroupUserOne($toUid, $friendGroupId)
    {
        $result = $this->where(['touid' => $toUid, 'friend_group_id' => $friendGroupId])->value('id');
        if (empty($result)) {
            //不存在好友分组用户时添加记录
            $this->data([
                'touid' => $toUid,
                'friend_group_id' => $friendGroupId,
            ]);
            $result = $this->save();
            if ($result) {
                return true;
            } else {
                return false;
            }
        } else {
            $this->where(['touid' => $toUid, 'friend_group_id' => $friendGroupId])->update(['status' => 1, 'update_time' => time()]);// 1是, 2否
            return true;
        }
    }

    /**
     * Notes:删除分组用户
     * User: tangfen
     * Date: 2023/12/23
     * Time: 16:20
     */
    public function delGroupFriendUser($id)
    {
        $result = $this->where(['id' => $id])->update(['status' => 2]);// 1是, 2否
        if ($result !== false) {
            return true;
        } else {
            return false;
        }
    }


}